<?php
/**
 * User: fwj
 * Date: 2018/6/14
 * Time: 10:13
 */

namespace app\index\controller;

use app\index\model\Grade as GradeModel;
use think\Request;

class Grade extends Base
{
    //班级列表
    public function gradeList()
    {
        $grade = new GradeModel();
        //获取班级数量
        $count = $grade->count();
        $grade = $grade::all();

        //班级信息
        foreach ($grade as $value) {
            $data = [
                'id' => $value->id,
                'name' => $value->name,
                'length' => $value->length,
                'price' => $value->price,
                'status' => $value->status,
                'create_time' => $value->create_time,
                //关联方式获取teacher
                'teacher' => isset($value->teacher->name) ? $value->teacher->name : '<span style="color:red">未分配</span>'
            ];
            $gradeList[] = $data;
        }

        $this->assign('title', '班级列表');
        $this->assign('keywords', '班级列表');
        $this->assign('desc', '班级列表');
        $this->assign('count', $count);
        $this->assign('gradeList', $gradeList);
        return $this->fetch('grade_list');
    }

    //更改状态
    public function setStatus(Request $request)
    {
        $id = $request->param('id');
        if (GradeModel::get($id)->getData('status') == 1) {
            GradeModel::update(['status' => 0], ['id' => $id]);
        } else {
            GradeModel::update(['status' => 1], ['id' => $id]);
        }
    }

    //班级编辑
    public function gradeEdit(Request $request)
    {
        $id = $request->param('id');
        $grade = GradeModel::get($id);
        $this->assign('title', '班级编辑');
        $this->assign('keywords', '班级编辑');
        $this->assign('desc', '班级编辑');
        $this->assign('grade_info', $grade);
        return $this->fetch('grade_edit');
    }

    //班级信息更新
    public function updateGrade(Request $request)
    {
        $grade_id = $request->param('id');
        $data = $request->except('id');
        $result = GradeModel::update($data,['id' => $grade_id]);
        dump($result);
        $
    }
}